
const mybehavior = require("../../../../behaviors/requests")
import WxValidate from "../../../../Wx_Validate/WxValidate"
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    base_urlstr: "https://logisticsengineering.gree.com/package_integrated_api/reverse_manage",
    employee_num: wx.getStorageSync('_id'),
    name: wx.getStorageSync('name'),
    phone_num: "",
    arrive_time: "",
    weight: "",
    address: "",
    material_type: "",
    material_source: "",
    popup_show0: false,
    options0: ["3吨", "10吨"],
    popup_show1: false,
    options1: ["3吨", "10吨"],

  },


  // 点击公司输入框
  onClick0(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show0: true
    })
  },
  // 选择公司输入框关闭
  onClose0() {
    this.setData({ popup_show0: false });
  },
  // 确定公司--弹出框关闭
  onChange0(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      weight: value,
      popup_show0: false,
    })
  },

  // 点击角色输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择角色输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  // 确定角色--弹出框关闭
  onChange1(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      weight: value,
      popup_show1: false,
    })
  },


  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },

  // 点击提交按钮
  formSubmit(e) {
    let that = this
    let url = that.data.base_urlstr + "/create_order";

    let data = {
      employee_num: this.data.employee_num,
      contact_person: this.data.contact_person,
      phone_num: this.data.phone_num,
      arrive_time: this.data.arrive_time,
      weight: this.data.weight,
      address: this.data.address,
      material_type: this.data.material_type,
      material_source: this.data.material_source,
    }
    console.log("123123",data);
    if (!that.WxValidate.checkForm(data)) {
      const error = that.WxValidate.errorList[0]
      Toast(error.msg)
    } else {
      that.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          this.formReset()
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
    // console.log(data)
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      contact_person: {
        required: true,
      },
      phone_num: {
        required: true,
      },
      arrive_time: {
        required: true,
      },
      weight: {
        required: true,
      },

    }

    let message = {
      contact_person: {
        required: "对接人不能为空",
      },
      phone_num: {
        required: "电话号码不能为空",
      },
      arrive_time: {
        required: "需求时间不能为空",
      },
      weight: {
        required: "需求吨位不能为空",
      },




    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 点击重置按钮
  formReset() {
    this.setData({
      factory: "",
      third_logistics: "",
      start_time_str: "",
      end_time_str: "",
      auth_scope: "",
      auth_image: "",
      supply_container_ids: "",
      recycle_list: "",
      uploader: [],
    })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})